import sys,os

if len(sys.argv)<2:
    print 'Usage: python gene2bed.py gene.lst'
    exit(1)

fi=open(sys.argv[1],'r')
for line in fi:
    line=line.strip()
    if len(line)<1:
    	continue
    fiG=open('genePred/'+line,'r')
    foB=open('genePred/'+line.split('.')[0]+'.bed','w')
    for data in fiG:
    	data=data.strip()
	if len(data)<1:
	    continue
    	temp=data.split('\t')
    	n=int(temp[9])
    	start=temp[10].split(',')
    	end=temp[11].split(',')
    	size=[]
    	for i in range(0,n):
    	    size.append(str(int(end[i])-int(start[i])))
    	    start[i]=str(int(start[i])-int(temp[1]))
    	temp[10]=','.join(size)+','
    	temp[11]=','.join(start)
    	foB.write('\t'.join(temp)+'\n')
    fiG.close()
    foB.close()
fi.close()
